package cn.rkylin.oms.common.base;

import cn.rkylin.apollo.common.util.BeanUtils;
import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.system.project.service.IProjectManagerService;
import cn.rkylin.oms.system.project.vo.ProjectVO;
import cn.rkylin.oms.system.shop.vo.ShopVO;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.util.List;

/**
 * 抽象适配器工厂
 *
 * @author wangxiaoyi
 */
public abstract class AbstractAdapterFactory {
    /**
     * 日志工具
     */
    private static final Log logger = LogFactory.getLog(AbstractAdapterFactory.class);

    @Autowired
    private static IProjectManagerService projectManagerService;

    /**
     * 根据shopId取得shop对象(支持缓存，缓存没有到库里找)
     *
     * @param shopId
     * @return 店铺VO
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
    public static ShopVO getShop(String shopId) {
        ShopVO shopVO = null;
        if (StringUtils.isEmpty(shopId)) {
            logger.debug("【shopId为空】");
            return null;
        }
//        RedisTemplate<String, String> redisTemplate = BeanUtils.getBean("redisTemplate");
//        Object obj = redisTemplate.opsForValue().get("shop:" + shopId);
//        if (obj != null && StringUtils.isNotBlank(obj.toString())) {
//            shopVO = (ShopVO) obj;
//            return shopVO;
//        }

        // 缓存里没有，从库里拿，再放缓存里。
        try {
            IDataBaseFactory dao = BeanUtils.getBean("dataBaseFactory");
            List<ShopVO> resultList = dao.findList("selectByPrimaryKeyShop", shopId);
            if (resultList != null && resultList.size() > 0) {
                shopVO = resultList.get(0);
//                RedisSerializer keySerializer = redisTemplate.getKeySerializer();
//                RedisSerializer valueSerializer = redisTemplate.getValueSerializer(); // 一定要用这个！！
//                redisTemplate.getConnectionFactory().getConnection().set(keySerializer.serialize("shop:" + shopId),
//                        valueSerializer.serialize(shopVO));
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        return shopVO;
    }

    /**
     * 根据prjId取得项目对象(支持缓存，缓存没有到库里找)
     *
     * @param prjId
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static ProjectVO getProject(String prjId) {
        ProjectVO projectVO = null;
        if (StringUtils.isEmpty(prjId)) {
            logger.debug("【prjId为空】");
            return null;
        }
        try {
            // 先在缓存里找，如果找到就返回
            RedisTemplate<String, String> redisTemplate = BeanUtils.getBean("redisTemplate");
            Object obj = redisTemplate.opsForValue().get("project:" + prjId);
            if (obj != null && StringUtils.isNotBlank(obj.toString())) {
                projectVO = (ProjectVO) obj;
                return projectVO;
            }

            // 缓存里找不到，去db找，并且缓存起来以备下次使用。
            if (projectManagerService == null) {
                projectManagerService = BeanUtils.getBean("projectManagerService");
            }
            projectVO = projectManagerService.findProjectById(prjId);
            if (projectVO == null) {
                return null;
            }
            RedisSerializer keySerializer = redisTemplate.getKeySerializer();
            RedisSerializer valueSerializer = redisTemplate.getValueSerializer(); // 一定要用这个！！
            redisTemplate.getConnectionFactory().getConnection().set(keySerializer.serialize("project:" + projectVO.getPrjId()), valueSerializer.serialize(projectVO));
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        return projectVO;
    }
}
